# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=aria2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.36.0
pkgrel=4
pkgdesc="A multi-protocol & multi-source, cross platform download utility (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://aria2.github.io/"
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
checkdepends=("${MINGW_PACKAGE_PREFIX}-cppunit")
optdepends=("${MINGW_PACKAGE_PREFIX}-ruby: aria2rpc and aria2mon")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/tatsuhiro-t/aria2/archive/release-${pkgver}.tar.gz)
sha256sums=('782b5dad48cb333e0cbef8795f868f7963455efecfd2a818feb8dab1cfa973f5')

prepare() {
  cd "${_realname}-release-${pkgver}"
  autoreconf -ifv || autoreconf -ifv
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  # disabling gnutls and wintls so we will fall back to openssl
  ../${_realname}-release-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --with-bashcompletiondir=${MINGW_PREFIX}/share/bash-completion/completions \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    --without-gnutls \
    --without-wintls \
    --enable-libaria2

  make
}

check() {
  cd build-${MSYSTEM}
  make check || true
}

package() {
  cd "build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  cd "../${_realname}-release-${pkgver}"

  install -Dm0755 doc/xmlrpc/aria2rpc "${pkgdir}"${MINGW_PREFIX}/bin/aria2rpc
  install -Dm0755 doc/xmlrpc/aria2mon "${pkgdir}"${MINGW_PREFIX}/bin/aria2mon
  echo '@ruby -- "%~dp0aria2rpc" %*' > "${pkgdir}"${MINGW_PREFIX}/bin/aria2rpc.cmd
  echo '@ruby -- "%~dp0aria2mon" %*' > "${pkgdir}"${MINGW_PREFIX}/bin/aria2mon.cmd
  rm ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/xmlrpc/aria2{rpc,mon}
}
